package com.mantru.iotpot.Home;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.mantru.iotpot.Cook.fragment.CustomBottomSheetDialogFragment;
import com.mantru.iotpot.DateClass.FoodList;
import com.mantru.iotpot.R;
import com.mantru.iotpot.Tools.ImageLoader;

import java.util.List;

public class GridAdapter extends BaseAdapter {

    private Context mContext;
    private List<FoodList> items;
    private LayoutInflater inflater;


    public GridAdapter(Context context, List<FoodList> items) {
        mContext = context;
        this.items = items;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mContext instanceof HomeActivity){
            FoodList food = items.get(position);
            if ("more".equals(food.getVegetableId())) { // 第十五项（从0开始计数）
                convertView = inflater.inflate(R.layout.run, null);//加载查看更多布局界面
                convertView.findViewById(R.id.run_go).setOnClickListener(v -> {
                    mContext.startActivity(new Intent(mContext, SearchActivity.class));
                });
            } else {
                convertView = inflater.inflate(R.layout.item1, null);
                TextView textView = convertView.findViewById(R.id.item_text);
                ImageView imageView = convertView.findViewById(R.id.item_img1);
                RelativeLayout box = convertView.findViewById(R.id.img_box);
                textView.setText(food.getVegetableName());
                ImageLoader.loadImage(food.getFaceImageUrl(), imageView);// 使用 ImageLoader 加载图片
                box.setOnClickListener(v -> {
//                    // 显示底部弹窗
                    CustomBottomSheetDialogFragment dialogFragment = new CustomBottomSheetDialogFragment();
                    dialogFragment.setVegetableId(food.getVegetableId());
                    Log.d("v_id","VegetableId: " + food.getVegetableId());
                    dialogFragment.show(((HomeActivity) mContext).getSupportFragmentManager(), "CustomBottomSheetDialog");
                });
            }
        }else if(mContext instanceof SearchActivity){
            convertView = inflater.inflate(R.layout.item1, null);
            TextView textView = convertView.findViewById(R.id.item_text);
            ImageView imageView = convertView.findViewById(R.id.item_img1);
            RelativeLayout box = convertView.findViewById(R.id.img_box);
            FoodList food = items.get(position);
            textView.setText(food.getVegetableName());
            ImageLoader.loadImage(food.getFaceImageUrl(), imageView);// 使用 ImageLoader 加载图片
            box.setOnClickListener(v -> {
                // 显示底部弹窗
                CustomBottomSheetDialogFragment dialogFragment = new CustomBottomSheetDialogFragment();
                dialogFragment.setVegetableId(food.getVegetableId());
                Log.d("v_id","VegetableId: " + food.getVegetableId());
                dialogFragment.show(((SearchActivity) mContext).getSupportFragmentManager(), "CustomBottomSheetDialog");
            });
        }
        return convertView;
    }
}

